/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年6月13日
 */
package com.massyframework.beanskin.launching.web.jetty;

import java.util.ArrayList;
import java.util.EventListener;
import java.util.List;

import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.webapp.WebAppContext;

import com.massyframework.beanskin.web.context.ServletContextWrapper;

/**
 * JettyServletContext适配器
 * @author huangkh
 *
 */
public class JettyServletContextAdapter extends ServletContextWrapper
	implements LifeCycle.Listener{
	
	private List<String> classNames;
	private List<EventListener> listeners;
	private List<Class<? extends EventListener>> classes;
	private WebAppContext context;

	/**
	 * @param context
	 */
	public JettyServletContextAdapter(WebAppContext context) {
		super(context.getServletContext());
		this.context = context;
		context.addLifeCycleListener(this);
	}

	@Override
	public synchronized void addListener(String className) {
		if (this.isStopped()) {
			if (this.classNames == null) {
				this.classNames = new ArrayList<>();
			}
			this.classNames.add(className);
		}else {
			super.addListener(className);
		}
		
	}

	@Override
	public <T extends EventListener> void addListener(T t) {
		if (this.isStopped()) {
			if (this.listeners == null) {
				this.listeners = new ArrayList<>();
			}
			this.listeners.add(t);
		}else {
			super.addListener(t);
		}
	}

	@Override
	public  void addListener(Class<? extends EventListener> listenerClass) {
		if (this.isStopped()) {
			if (this.classes == null) {
				this.classes = new ArrayList<>();
			}
			this.classes.add(listenerClass);
		}else {
			super.addListener(listenerClass);
		}
	}
	
	protected boolean isStopped() {
		return this.context.isStopped();
	}

	protected boolean isStarting() {
		return this.context.isStarting();
	}
	
	protected boolean isStarted() {
		return this.context.isStarted();
	}

	@Override
	public synchronized void lifeCycleStarting(LifeCycle event) {
		if (this.classNames != null) {
			for (String className: this.classNames) {
				this.getTargetServletContext().addListener(className);
			}
			this.classNames.clear();
		}
		
		if (this.listeners != null) {
			for (EventListener listener: this.listeners) {
				this.getTargetServletContext().addListener(listener);
			}
			this.listeners.clear();
		}
		
		if (this.classes != null) {
			for (Class<? extends EventListener> clazz: this.classes) {
				this.getTargetServletContext().addListener(clazz);
			}
			this.classes.clear();
		}
	
	}

	@Override
	public void lifeCycleStarted(LifeCycle event) {
		
	}

	@Override
	public void lifeCycleFailure(LifeCycle event, Throwable cause) {
	
	}

	@Override
	public void lifeCycleStopping(LifeCycle event) {
		
	}

	@Override
	public void lifeCycleStopped(LifeCycle event) {
		
	}
}
